package com.wtx.security.common;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.RedisTemplate;

import java.io.Serializable;
import java.util.concurrent.TimeUnit;

public class RedisService {
	@Autowired
	@Qualifier("stringRedisTemplate")
	private RedisTemplate<String, String> stringRedisTemplate;

	@Autowired
	@Qualifier("jacksonRedisTemplate")
	private RedisTemplate<String,Object> jacksonRedisTemplate;

	@Autowired
	@Qualifier("defaultRedisTemplate")
	private RedisTemplate<Serializable,byte[]> defaultRedisTemplate;

	public RedisTemplate<String, String> getStringRedisTemplate() {
		return stringRedisTemplate;
	}

	public RedisTemplate<String, Object> getJacksonRedisTemplate() {
		return jacksonRedisTemplate;
	}

	public RedisTemplate<Serializable, byte[]> getDefaultRedisTemplate() {
		return defaultRedisTemplate;
	}

	public long del(final byte[] key) throws Exception{
		return (long) getStringRedisTemplate().execute(new RedisCallback<Object>() {
			public Long doInRedis(RedisConnection connection) {
				return connection.del(key);
			}
		});
	}

	public static void main(String[] args) {
	}


	
    /**
     * 设置过期时间
     * @param key
     * @param time
     * @param unit
     * @return
     */
   public boolean expire(String key, long time, TimeUnit unit) throws Exception{
       return getStringRedisTemplate().expire(key, time, unit);
   }


}
